using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using APDocConvNET;
using System.IO;
using Utility = PHSRAG.Utility;

namespace InsightServiceUtility
{
    public class DocumentHelper
    {

        public static string PDFConversionTimeOut = "PDFConversionTimeout";
        public static int PDFConversionDefaultTimeOut = 90;
        public static string ConvertToPDF(string filename, params object[] args)
        {
            // Variables
            int timeout = (int)args[0];
            // Name of image file to convert
            string fileName = filename;

            // Name of PDF file to create
            string pdfFileName = Path.ChangeExtension(filename, ".PDF");

            string tempPath = Path.GetTempPath();

            string inputDirectory = Path.Combine(tempPath, "Input");
            Directory.CreateDirectory(inputDirectory);

            string outDirectory = Path.GetDirectoryName(pdfFileName);
            //string outDirectory = Path.Combine(tempPath, "Out");
            //Directory.CreateDirectory(outDirectory);

            string errorDirectory = Path.Combine(tempPath, "Error");
            Directory.CreateDirectory(errorDirectory);

            string originalDirectory = Path.Combine(tempPath, "Original");
            Directory.CreateDirectory(originalDirectory);

            string ip = "127.0.0.1";

            int port = 0;


            APDocConverter converter = new APDocConverter();            
            converter.StatusTimeout = (short)timeout;

            //Utility.ExceptionReport.WriteToEventLog("converte-submit", "", PHSRAG.Utility.ExceptionReport.ReportType.Information);
            int returnCode = converter.Submit(ip, 0, filename, outDirectory, errorDirectory, Path.GetDirectoryName(filename), "", "", 0, "");
            //Utility.ExceptionReport.WriteToEventLog("converte-submit COMPLETE", "", PHSRAG.Utility.ExceptionReport.ReportType.Information);
            // Check for errors on submission and write to log
            if (returnCode != 0)
            {
                Utility.ExceptionReport.WriteToEventLog("Submit status failure " + filename, returnCode.ToString(), Utility.ExceptionReport.ReportType.Error);
                Utility.EmailNotification.SendEmail("Error from converter.Submit method \r Return Code:" + returnCode.ToString() + "\r Filename=" + filename);
            }			

            // Check the status of the conversion
            returnCode = converter.CheckStatus(ip, port, filename, outDirectory, errorDirectory);
            // Check for errors on the conversion and write to log
            if (returnCode != 1)
            {
                Utility.ExceptionReport.WriteToEventLog("CheckStatus failure " + filename, returnCode.ToString(), Utility.ExceptionReport.ReportType.Error);
                Utility.EmailNotification.SendEmail("Converter.CheckStatus method \r Return Code:" + returnCode.ToString() + "\r Filename=" + filename);
            }            
            converter = null;
            //return Path.Combine(outDirectory, Path.GetFileName(pdfFileName));
            return pdfFileName;
        }

        /// <summary>
        /// Read the files as byte array
        /// </summary>
        /// <param name="fullFilePath">Full Path of the file to be read</param>
        /// <returns>byte array of the file content</returns>
        public static byte[] GetBytesFromFile(string fullFilePath)
        {
            // this method is limited to 2^32 byte files (4.2 GB)
            byte[] bytes = null;           
            try
            {
                using (FileStream fs = new FileStream(fullFilePath, FileMode.Open,  FileAccess.Read))
                {
                    bytes = new byte[fs.Length];
                    fs.Read(bytes, 0, Convert.ToInt32(fs.Length));
                    fs.Close();                    
                }
            }
            catch (Exception ex)
            {
                Utility.ExceptionReport.WriteToEventLog("GetBytesFromFile ():\r Error: ", ex.Message, Utility.ExceptionReport.ReportType.Error);
                Utility.EmailNotification.SendEmail("GetBytesFromFile ():\r Error: " + ex.Message);
            }
            return bytes;
        }

        
    }
}
